package com.example.pmanagesys.service;

import com.example.pmanagesys.controller.dto.UserDto;
import com.example.pmanagesys.entity.User;
import com.example.pmanagesys.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;

@Service  //@Service 将 Service 注入到springboot容器
public class UserService {

    @Autowired //引入
    private UserMapper userMapper;

    // 判断新增还是更新
    public int save(User user) {
        if (user.getId() == null) {//user没有id,则表示为新增
            return userMapper.insert(user);
        } else {//否则为更新
            return userMapper.update(user);
        }
    }

    // 登录,数据库中姓名唯一  用户名唯一
    public boolean login(UserDto userDTO, HttpSession session) {
        User user = userMapper.findOne(userDTO);// 调用数据库查询方法
        // 判断是否找到了用户记录，如果找到了返回true，否则返回false
        if(user != null){
            session.setAttribute("user", userDTO.getUsername());  // 添加用户
            return  true;
        }else {
            return false;
        }
    }


}
